package validation

import (
	"context"
	"errors"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"mall_admin_api/biz/dal"
	"mall_admin_api/biz/model"
	"mall_admin_api/biz/model/admin_orders"
	"mall_admin_api/biz/model/common"

	handlerCommon "mall_admin_api/biz/handler/common"
)

type OrderValidation struct {
	common *handlerCommon.Response
	dao    *dal.InitDAO
}

func NewOrderValidation(f *handlerCommon.Response) *OrderValidation {
	return &OrderValidation{
		dao:    dal.NewInitDAO(f.GetBizInfo().ID),
		common: f,
	}
}

func (o *OrderValidation) CheckDeliveryOrders(ctx context.Context, req *admin_orders.DeliveryOrderRequest) (*model.Order, error) {
	data, err := o.dao.OrderDAO.GetOrderInfoByID(ctx, req.GetOrderID())
	if err != nil {
		hlog.CtxErrorf(ctx, "GetOrderInfo failed: %v", err)
		return nil, err
	}
	if data.Status != common.OrderStatus_Paid {
		oErr := errors.New("订单不是待发货状态")
		hlog.CtxErrorf(ctx, "Order status is %d, err %s", data.Status, oErr)
		return nil, oErr
	}
	return data, nil
}

func (o *OrderValidation) CheckRemarkOrders(ctx context.Context, req *admin_orders.RemarkOrderRequest) (*model.Order, error) {
	data, err := o.dao.OrderDAO.GetOrderInfoByID(ctx, req.GetID())
	if err != nil {
		hlog.CtxErrorf(ctx, "GetOrderInfo failed: %v", err)
		return nil, err
	}
	return data, nil
}
